package cn.tedu.flower.manage.content.controller;

import cn.tedu.flower.manage.common.web.JsonResult;
import cn.tedu.flower.manage.content.pojo.param.BannerAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.BannerUpdateInfoParam;
import cn.tedu.flower.manage.content.pojo.vo.BannerListItemVO;
import cn.tedu.flower.manage.content.service.IBannerService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理轮播图相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/content/banner")
@Validated
@Api(tags = "2.6. 内容管理-轮播图管理")
public class BannerController {

    @Autowired(required = false)
    private IBannerService bannerService;

    public BannerController() {
        log.debug("创建控制器类对象：BannerController");
    }

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/content/banner/add-new')")
    @ApiOperation("添加轮播图")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid BannerAddNewParam bannerAddNewParam) {
        log.debug("开始处理【添加类别】的请求，参数：{}", bannerAddNewParam);
        bannerService.addNew(bannerAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/content/banner/delete')")
    @ApiOperation("根据ID删除轮播图")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "轮播图ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的轮播图ID值！") Long id) {
        log.debug("开始处理【根据ID删除轮播图】的请求，参数：{}", id);
        bannerService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/content/banner/update')")
    @ApiOperation("修改轮播图详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "轮播图ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的轮播图ID值！") Long id,
                                     @Valid BannerUpdateInfoParam bannerUpdateInfoParam) {
        log.debug("开始处理【修改轮播图详情】的请求，ID：{}，新数据：{}", id, bannerUpdateInfoParam);
        bannerService.updateInfoById(id,bannerUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/content/banner/read')")
    @ApiOperation("查询轮播图列表")
    @ApiOperationSupport(order = 420)
    public JsonResult listTree() {
        log.debug("开始处理【获取类别树】的业务，参数：无");
        List<BannerListItemVO> bannerList = bannerService.list();
        return JsonResult.ok(bannerList);
    }
}
